package com.angzh.bolibao.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class User extends Model<User>{
	
	public final static User dao = new User();
	
	/**
	 * 根据用户名查找用户是否存在
	 * @param userName
	 * @return User
	 * 
	 * **/
	public User getUserByUserName(String userName){
		return this.dao.findFirst("select * from user where username=?", userName);
	}
	/**
	 * 用户登录信息查询
	 * @param userName
	 * @param password
	 * @return User
	 * **/
	public User getUserByLogin(String userName,String password){
		return this.dao.findFirst("select * from user where username=? and password=?", new Object[]{userName,password});
	}
	public Page<User> paginate(int pageNum, int pageSize, int role){
		return this.dao.paginate(pageNum, pageSize, "select *", "from user where role="+role+" order by id desc");
	}
	public Page<User> paginate(int pageNum, int pageSize, int role,String company){
		return this.dao.paginate(pageNum, pageSize, "select * ", "from user where role="+role+" and company='"+company+"'");
	}
	public List<User> userList(int role,String company){
		return this.dao.find("select * from user where role = "+role+" and company='"+company+"'");
	}
	public User getUserByWeichat(String weichat){
		return this.dao.findFirst("select * from user where weichat='"+weichat+"'");
	}
	public List<User> companyList(){
		return this.dao.find("select company from user where role=2");
	}
	public Record companyCommission(String company,int type,String startTime,String endTime){
		String sql1 = "";
		if(startTime!=null&&!startTime.equals("")){
			sql1 += " and buytime >= '"+startTime+" 00:00:00'";
		}
		if(endTime!=null && !endTime.equals("")){
			sql1 += " and buytime <= '"+endTime+" 23:59:59'";
		}
		Record data = Db.findFirst("select count(c.id) as count  from customer c,user u "
				+ "where c.recommended=u.weichat and u.company='"+company+"' "
				+ "and c.type="+type+sql1);
		return data;
	}
	public User getCompanyByName(int role,String company){
		return this.findFirst("select * from user where role="+role+" and company='"+company+"'");
	}
}
